package com.cdks.wuxiu.api.service.mysql;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.cdks.wuxiu.api.mapper.UserLoginInfoMapper;
import com.cdks.wuxiu.entity.mysql.UserLoginInfo;

import org.springframework.stereotype.Service;


/**
 * 用户登录信息表 服务实现类
 */
@Service
public class UserLoginInfoServiceImpl extends ServiceImpl<UserLoginInfoMapper, UserLoginInfo> implements UserLoginInfoService {

    @Override
    public int insertLoginInfo(UserLoginInfo loginInfo) {
        int ret  = baseMapper.insert(loginInfo);
        if (ret > 0)
        {
            return 0;
        }

        return -1;
    }

    @Override
    public int updateLoginInfo(UserLoginInfo loginInfo) {
        UpdateWrapper<UserLoginInfo> wrapper = new UpdateWrapper<>();
        wrapper.lambda().eq(UserLoginInfo::getUserId, loginInfo.getUserId());
        baseMapper.update(loginInfo, wrapper);

        return 0;
    }

    @Override
    public UserLoginInfo getByUserId(long userId) {
        QueryWrapper<UserLoginInfo> wrapper = new QueryWrapper<>();
        wrapper.lambda().eq(UserLoginInfo::getUserId, userId);
        
        return baseMapper.selectOne(wrapper);
    }

    @Override
    public int updatePushToken(long userId, String token) {
        UserLoginInfo loginInfo  = new UserLoginInfo();
        loginInfo.setPushToken(token);

        UpdateWrapper<UserLoginInfo> wrapper = new UpdateWrapper<>();
        wrapper.lambda().eq(UserLoginInfo::getUserId, userId);

        int ret  = baseMapper.update(loginInfo, wrapper);
        if (ret == 1)
        {
            return 0;
        }

        return -1;
    }

    @Override
    public int updatePushToken(long userId, String token, int status) {
        UserLoginInfo loginInfo  = new UserLoginInfo();
        loginInfo.setPushToken(token);
        loginInfo.setPushStatus(status);

        UpdateWrapper<UserLoginInfo> wrapper = new UpdateWrapper<>();
        wrapper.lambda().eq(UserLoginInfo::getUserId, userId);

        int ret  = baseMapper.update(loginInfo, wrapper);
        if (ret == 1)
        {
            return 0;
        }

        return -1;
    }
}
